package com.ithe.shopweb.controller;

import com.ithe.shopweb.common.Constants;
import com.ithe.shopweb.common.Result;
import com.ithe.shopweb.pojo.Customer;
import com.ithe.shopweb.pojo.Manager;
import com.ithe.shopweb.pojo.resp.CustomerResp;
import com.ithe.shopweb.pojo.resp.ManageResp;
import com.ithe.shopweb.serve.LoginServe;
import com.ithe.shopweb.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 功能：
 * 日期： 2024/5/21 23:32
 */
@RestController
@RequestMapping("/login")
public class LoginController implements Constants {

    @Autowired
    private LoginServe loginServe;

    @PostMapping("/customer")
    public Result loginCustomer(@RequestBody Customer customer) {
        String username = customer.getUsername();
        String password = customer.getPassword();
        CustomerResp customerData = loginServe.queryCustomer(username, password);
        if (customerData == null) {
            return Result.error(CODE_600, "账户密码错误");
        } else {
            customerData.setNowDate(new Date());
            customerData.setToken(TokenUtils.genToken(customerData.getUserID(), password));
            return Result.success(customerData);
        }
    }

    @PostMapping("/manage")
    public Result loginManage(@RequestBody Manager manager) {
        String adminName = manager.getAdminName();
        String password = manager.getPassword();
        ManageResp manageData = loginServe.queryManage(adminName, password);
        if (manageData == null) {
            return Result.error(CODE_600, "账户密码错误");
        } else {
            manageData.setNowDate(new Date());
            manageData.setToken(TokenUtils.genToken(manageData.getAdminID(), password));
            return Result.success(manageData);
        }
    }

}
